/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.controllers.admin;

import app.Const;
import app.models.DaoKit;
import app.models.info.Floor;
import app.models.info.ShoppingMall;
import app.models.brand.Brand;
import app.models.brand.BrandFormat;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-04-12 15:13
 * @since JDK 1.6
 */
public class BrandController extends BasicController {


    public static final String FORM_ATTR = "brand";

    /**
     * The index route.
     */
    public void index() {
        List<Floor> floors = Floor.dao.list();
        List<BrandFormat> formats = BrandFormat.dao.list();

        setAttr("floors", floors);
        setAttr("formats", formats);
        render("index.ftl");
    }


    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        String name = getPara("name");
        Integer floor = getParaToInt("floor");
        Integer format = getParaToInt("format");
        Page<Brand> parkingPage = Brand.dao.datatable(criterias, name, floor, format);

        renderDataTables(parkingPage, criterias);
    }


    public void create() {
        Brand parking = new Brand();
        setAttr(FORM_ATTR, parking);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr("floors", Floor.dao.findByNotParking());
        setAttr("formats", BrandFormat.dao.list());
        setAttr(Const.ACTION_ATTR, Const.CREATE_ACTION);
        render("item.ftl");
    }


    public void edit() {
        final int id = getParaToInt(0, 1);
        Brand brand = Brand.dao.findById(id);
        String pictures = brand.getStr("picture");
        if (!Strings.isNullOrEmpty(pictures)) {
            final List<String> pics = AppFunc.COMMA_SPLITTER.splitToList(pictures);
            setAttr("pics", pics);
        }

        setAttr(FORM_ATTR, brand);
        setAttr("mallblock", ShoppingMall.dao.malllist());
        setAttr("floors", Floor.dao.findByNotParking());
        setAttr("formats", BrandFormat.dao.list());
        setAttr(Const.ACTION_ATTR, Const.EDIT_ACTION);
        render("item.ftl");
    }


    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("brand.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }


    public void save() {
        Brand brand = getModel(Brand.class);
        final String format_name = brand.getStr("format_name");
        brand.set("booking", BooleanUtils.toBooleanObject(getPara("booking", "off")));
        if (!Strings.isNullOrEmpty(format_name)) {
            List<String> format_vals = AppFunc.COMMA_SPLITTER.splitToList(format_name);
            brand.set("format", format_vals.get(0));
            brand.set("format_name", format_vals.get(1));
        }
        if (DaoKit.isNew(brand)) {
            brand.save();
        } else {
            brand.update();
        }
        redirect("/admin/brand");
    }


    public void merchant() {
        String merchant = getPara("merchant");
        if (StringUtils.equals(merchant, "merchant-repast")) {
            List<Brand> brands = Brand.dao.findByCatetory(Const.REPAST);
            renderJson(AjaxMessage.ok(brands));
        } else if (StringUtils.equals(merchant, "merchant-ktv")) {
            List<Brand> brands = Brand.dao.findByCatetory(Const.KTV);
            renderJson(AjaxMessage.ok(brands));
        }
    }
}